<?php
namespace app\manage\controller;

class GameCp extends Base
{
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $id = input('id');
            $appId = input('app_id');

            $where['status'] = ['<>', -1];
            !empty($appId) && $where[] = ['exp',"FIND_IN_SET({$appId}, app_id)"];
            !empty($id) && $where['id'] = $id;


            if( ! $count=model('CpAccount')->where($where)->count() ){
               $this->layTable();
            }

            $select = model('CpAccount')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('id desc')
                ->select();

            $data = [];
            $appIdName = changeValKey('App', 'app_id', 'app_name');
            foreach ($select as $k=>$v){
                $appStr = '';

                if($v['app_id']){
                    $appArr = explode(',', $v['app_id']);
                    foreach ($appArr as $kk=>$vv){
                        $appStr .= $appIdName[ $vv ] . ',';
                    }
                    $appStr = rtrim($appStr, ',');
                }

                $data[] = [
                    'id' => $v['id'],
                    'account' => $v['account'],
                    'username' => $v['username'],
                    'login_time' => $v['login_time'] ? date('Y-m-d H:i:s', $v['login_time']) : '',
                    'login_ip' => $v['login_ip'],
                    'remark' => $v['remark'],
                    'status' => $v['status'],
                    'app_name' => $appStr,
                ];
            }

           $this->layTable(0, 'CP列表', $data, $count);
        }else{
            $appId = model('App')->where('status', '<>', -1)->order('app_id desc')->select();
            return view('', ['app_id' => $appId]);
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            $data = input();
            $data['password'] = md5(input('password'));
            $data['password_confirm'] = md5(input('password_confirm'));
            $addRs = model('CpAccount')
                ->validate([
                    'username' => 'unique:CpAccount',
                    'account' => 'unique:CpAccount',
                    'password'=>'confirm'
                ], [
                    'username.unique' => 'CP名称已存在',
                    'account.unique' => '登录账号已存在',
                    'password.confirm' => '两次密码不一致'
                ])->allowField(true)->save( $data );

            $addRs ? $this->layJson(0, '添加成功') : $this->layJson(1, model('CpAccount')->getError());
        }else{
            $appId = model('App')->where('status', '<>', -1)->order('app_id desc')->select();
            return view('', ['app_id' => $appId]);
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $data = input();

            if(!empty($data['password'])){
                $data['password'] = md5($data['password']);
                $data['password']!==md5($data['password_confirm']) && $this->layJson(1, '两次密码不一致');
            }else{
                unset($data['password']);
                unset($data['password_confirm']);
            }

            $editRs = model('CpAccount')
                ->validate([
                    'username' => 'unique:CpAccount',
                    'account' => 'unique:CpAccount',
                ], [
                    'username.unique' => 'CP名称已存在',
                    'account.unique' => '登录账号已存在',
                ])->allowField(true)->isUpdate(true)->save( $data );

            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, model('CpAccount')->getError());
        }else{
            $find = model('CpAccount')->where('id', input('id'))->find();
            $appId = model('App')->where('status', '<>', -1)->order('app_id desc')->select();
            return view('', ['app_id' => $appId, 'find'=>$find]);
        }
    }

    //删除
    public function del(){
        $data['id'] = input('id');
        $data['status'] = -1;
        $delRs = model('CpAccount')->isUpdate(true)->save( $data );
        $delRs!==false ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }


}